<template>
<div>
    <div >
       <el-button size="small" @click="Addlaber">保存</el-button>
       <el-button size="small" v-if="Yes" @click="examine">提交审批</el-button>
      </div>

    <div >
        <font size="6" bold   style="position:relative;left:700px">项目工资登记</font>
      </div>
      <br>
      
<el-form :model="apply"  label-width="100px" class="demo-ruleForm">
    <!-- 第一个表格 -->
    <table class="divtable" width="100%">
        <tr>
            <td colspan="8" class="color">基本信息</td>
        </tr>
        <tr>
            
            <td class="tabletd">编号:</td>
            <td><el-input v-model="apply.id" placeholder="保存后自动生成" disabled></el-input></td>
            <td class="tabletd">所属项目:</td>
            <td ><el-input v-model="apply.p_id" @focus="pid"   clearable></el-input></td>
            <td class="tabletd">工资类别:</td>
            <td>
                <el-select v-model="apply.type" clearable placeholder="请选择" >
                    <el-option value="自由管理人员人工费">自由管理人员人工费</el-option>
                    <el-option value="自由工人人工费">自由工人人工费</el-option>
                    <el-option value="零星外部雇工人工费">零星外部雇工人工费</el-option>
                    <el-option value="外部分包工人人工费">外部分包工人人工费</el-option>
                </el-select>
            </td>
            <td class="tabletd">年度:</td>
            <td><el-input  v-model="apply.year" oninput="value=value.replace(/[^\d]/g,'')" maxLength='4' clearable></el-input></td>
        </tr>
        <tr>
            <td class="tabletd">月份:</td>
            <td><el-input  v-model="apply.menth" oninput="value=value.replace(/[^\d]/g,'')" maxLength='2' clearable ></el-input></td>
            <td class="tabletd">登记日期:</td>
            <td><el-input type="date" v-model="apply.apply_date"  value-format="yyyy-MM-dd" ></el-input></td>
            <td class="tabletd">填报人:</td>
            <td><el-input v-model="apply.handler_id"   clearable></el-input></td>
            <td class="tabletd"></td>
            <td></td>
        </tr>
        
    </table>

    <multi-file-upload
                style="width: 100%;margin: 20px auto"
                title="附件"
                action="http://localhost/uploads"
                :params="{folder: 'tenderPurchasePlan'}"
                :headers="{token: token}"
                :show-button="!checkRoute"
                @uploadSuccess="handleUploadSuccess"
                :parent-file-list="fileList"
        ></multi-file-upload>

    <!-- 多行添加 -->
    <div style="width: 100%;margin: 30px auto">
            <div style="width: 100%;">
                <div style="font-weight: 700;font-size: 20px;display: flex">
                    <div>|工资明细</div>
                    <div style="margin-left: 30px">
                        <el-button v-if="!checkRoute" size="mini" type="primary" @click="handleAddRow">增行</el-button>
                    </div>
                </div>
            </div>
            <el-divider></el-divider>
            <el-table :data="tenderPurchaseDetails">
                <el-table-column prop="index" v-if="false"></el-table-column>
                <el-table-column prop="mateId" v-if="false"></el-table-column>
                <el-table-column label="姓名" prop="name">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.name" ></el-input>
                    </template>
                </el-table-column>
                
                <el-table-column label="工种" prop="type">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.type"  ></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="出勤天数" prop="day">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.day" type="number"></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="日工资" prop="salaryday">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.salaryday"  type="number"></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="基本工资" prop="salary">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.salary"  type="number"></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="加班费" prop="salaryplus">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.salaryplus"  type="number"></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="借款" prop="borrowsalary">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.borrowsalary"  type="number"></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="保险" prop="insurancesalary">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.insurancesalary"  type="number"></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="扣款" prop="deductsalary">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.deductsalary"  type="number"></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button v-if="!checkRoute" type="danger" @click="handleRemoveRow(scope.row)">删行</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

</el-form>


    <!--所属项目子组件-->
<el-dialog  :visible.sync="dialogVisible"  width="50%">
<project-info-list  v-if="dialogVisible"  @row="project"></project-info-list>
</el-dialog>





  </div>
</template>

<script>
import ProjectInfoList from '../common/projectInfo/ProjectInfoList.vue';
import {DateUtil} from "@/model/common/DateUtil";

import LaoWuHeTong from '@/model/smj/LaoWuHeTong'
 const Laowuhetong= new LaoWuHeTong;

import XingMuGongZi from '@/model/smj/XingMuGongZi'
const xiangmugongzi = new XingMuGongZi;

export default {
  name: "JinDuKuanShenBao",
  components: {ProjectInfoList},
  directives: {},
  data() {
    return {
        apply:{
            apply_date:DateUtil.today(),
            handler_id:sessionStorage.getItem('realName')
        },
        tenderPurchasePlan: {
            id: '',
            date: DateUtil.today(),
            planEditor: sessionStorage.getItem('realName')
        },
        fileList:[],
        projectId:'',//所属项目id


        dialogVisible:false,//隐藏子组件

        tenderPurchaseDetails:[],
        Yes:false,
    }
  },
  mounted() {},
  methods: {

      //开启隐藏项目子组件
      pid(){
           this.dialogVisible = true;
      },

      //响应项目组件
      project(row){
            this.apply.p_id = row.projectName;
            this.projectId = row.id;//获取所属项目id
            this.dialogVisible = false;
      },

     

      //添加一行
      handleAddRow() {
                this.tenderPurchaseDetails.push({
                    index: '',
                    name: '',
                    type: '',
                    day: '',
                    salaryday: '',
                    salary:'',
                    salaryplus:'',
                    borrowsalary:'',
                    insurancesalary:'',
                    deductsalary:'',
                })
            },
        
        //删行
        handleRemoveRow(row) {
            this.tenderPurchaseDetails.splice(row.index, 1)
         },


         //文件上传
        handleUploadSuccess(ids,fileList){
             console.log(fileList)
                this.tenderPurchasePlan.attachemeIds = ids
            },



     

      //添加劳务合同
      Addlaber(){
          console.log(this.apply)
            //判断合同信息不能为空
          
          if(this.apply.p_id == '' || this.apply.p_id == undefined){
                this.$message.error('错误信息,所属项目不能为空');
          }else{
              if(this.apply.type == '' || this.apply.type == undefined){
                this.$message.error('错误信息,工资类别不能为空');
          }else{
              if(this.apply.year == '' || this.apply.year == undefined){
                this.$message.error('错误信息,年度不能为空');
          }else{
              if(this.apply.menth == '' || this.apply.menth == undefined){
                this.$message.error('错误信息,月份不能为空');
          }else{
              if(this.apply.handler_id == '' || this.apply.handler_id == undefined){
                this.$message.error('错误信息,登记人不能为空');
          }else{
              //判断工程量清单
                 let index2 =0;
                for (let index = 0; index < this.tenderPurchaseDetails.length; index++) {
                    if(this.tenderPurchaseDetails[index].name == ''){
                        this.$message.error('错误信息,姓名不能为空');
                }else{
                    if(this.tenderPurchaseDetails[index].type == ''){
                        this.$message.error('错误信息,工种不能为空');
                }else{
                    if(this.tenderPurchaseDetails[index].day == ''){
                        this.$message.error('错误信息,出勤天数不能为空');
                }else{
                    if(this.tenderPurchaseDetails[index].salaryday == ''){
                        this.$message.error('错误信息,日工资不能为空');
                }else{
                     if(this.tenderPurchaseDetails[index].salary == ''){
                        this.$message.error('错误信息,基本工资不能为空');
                }else{
                     if(this.tenderPurchaseDetails[index].salaryplus == ''){
                        this.$message.error('错误信息,加班费不能为空');
                }else{
                     if(this.tenderPurchaseDetails[index].borrowsalary == ''){
                        this.$message.error('错误信息,借款不能为空');
                }else{
                    if(this.tenderPurchaseDetails[index].insurancesalary == ''){
                        this.$message.error('错误信息,保险不能为空');
                }else{
                    if(this.tenderPurchaseDetails[index].deductsalary == ''){
                        this.$message.error('错误信息,扣钱不能为空');
                }else{
                    index2++;
                }
                }
                }
                }
                }
                }
                }
                }
                }
                }

                 if(index2 == this.tenderPurchaseDetails.length ){
                     this.apply.attach_ids=this.tenderPurchasePlan.attachemeIds;
                     console.log(this.apply.attach_ids)
                          xiangmugongzi.AddLaberSalary(this.apply,this.projectId).then((response)=>{
                               this.$message({ message: '添加成功',  type: 'success' });
                               this.Yes=true;
                               let id = response.data;
                               this.apply.id=id;
                               console.log(id)
                              if(this.tenderPurchaseDetails.length>0){
                                    for (let index = 0; index < this.tenderPurchaseDetails.length; index++) {
                                        xiangmugongzi.AddWorkList(this.tenderPurchaseDetails[index],id).then(()=>{}) 
                                    }
                              }
                              if(this.laber.attach_ids != '' || this.laber.attach_ids != undefined){
                                  Laowuhetong.UpdateInfo(this.laber.attach_ids,sessionStorage.getItem('realName')).then(()=>{})
                              }
                          })
                 }
          }
          }
          }
          }
      }

      },

      //提交审批
      examine(){
            xiangmugongzi.FlowSalary(this.apply).then((response)=>{
                if(response.data == 1){
                    this.$message({ message: '提交成功',  type: 'success' });
                     this.Yes=false;
                }else{
                    this.$message.error('提交失败');
                }
                
            }) 
      },

         //测试
    //   Addlaber2(){
    //       this.apply.id='20210209123712'
    //     xiangmugongzi.FlowSalary(this.apply).then((response)=>{
    //             if(response.data == 1){
    //                 this.$message({ message: '提交成功',  type: 'success' });
    //                  this.Yes=false;
    //             }else{
    //                 this.$message.error('提交失败');
    //             }
                
    //         }) 
    //   }

  },
  computed: {
            token: {
                get() {
                    return this.$store.state.token
                }
            },
            checkRoute(){
                return Object.keys(this.$route.params).length != 0
            }
        },
  created() {
       
  },
  
};
</script>

<style>
.divtable{
    border-collapse:collapse;
}
.divtable td{
    padding:10px;
    border:1px solid rgb(7, 136, 175);
}
.tabletd{
    text-align: right;
    background-color: rgb(113, 222, 241);
}
.color{
    background-color: rgb(113, 222, 241);
}
.divtable2{
    border-collapse:collapse;
}
.divtable2 td{
    padding:10px;
    border:1px solid rgb(7, 136, 175);
}
.td{
    text-align: center;
}
</style>